// 脚本：批量插入首页演示作品数据到 Post 表
const mongoose = require('mongoose');
const Post = require('./src/models/Post');
const User = require('./src/models/User');
const connectDB = require('./src/config/db');

async function seedPosts() {
  await connectDB();
  // 清空旧数据
  await Post.deleteMany({});

  // 查找昵称为“派大星”的用户
  let user = await User.findOne({ nickname: '派大星' });
  if (!user) {
    // 若不存在则创建一个管理员用户
    user = await User.create({
      email: 'admin@school.com',
      nickname: '派大星',
      password: 'admin123',
      role: 'admin',
      avatar: '/default-avatar.png',
      isVerified: true
    });
  }

  await Post.insertMany([
    // 通知
    { title: '2025年暑假放假安排', content: '暑假时间为7月10日至8月31日', category: '通知', author: user._id, cover: '', mediaType: 'text', tags: ['放假'], views: 120, likes: 10 },
    { title: '校园网络维护', content: '6月15日凌晨校园网将短暂中断', category: '通知', author: user._id, cover: '', mediaType: 'text', tags: ['网络'], views: 80, likes: 5 },
    // 话题
    { title: '考研经验交流', content: '欢迎分享你的考研心得', category: '话题', author: user._id, cover: '', mediaType: 'text', tags: ['考研'], views: 200, likes: 30 },
    { title: '二手书交易', content: '毕业季二手书低价转让', category: '话题', author: user._id, cover: '', mediaType: 'text', tags: ['二手'], views: 150, likes: 20 },
    // 活动
    { title: '篮球友谊赛', content: '6月20日体育馆举行篮球赛', category: '活动', author: user._id, cover: '', mediaType: 'image', tags: ['篮球'], views: 90, likes: 8 },
    { title: '摄影大赛', content: '校园风光摄影比赛，投稿截止6月30日', category: '活动', author: user._id, cover: '', mediaType: 'image', tags: ['摄影'], views: 110, likes: 12 },
    // 市场
    { title: '九成新考研资料', content: '低价转让考研资料', category: '市场', author: user._id, cover: '', mediaType: 'image', tags: ['考研'], views: 60, likes: 3 },
    { title: '二手自行车', content: '自用二手车，价格可议', category: '市场', author: user._id, cover: '', mediaType: 'image', tags: ['二手'], views: 70, likes: 4 },
    // 作品（图片/视频）
    { title: '春日校园摄影', content: '春天的校园美景', category: '作品', author: user._id, cover: 'https://picsum.photos/seed/photo1/400/300', mediaType: 'image', tags: ['摄影'], views: 300, likes: 50 },
    { title: '毕业季短片', content: '记录青春的毕业短视频', category: '作品', author: user._id, cover: 'https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4', mediaType: 'video', tags: ['视频'], views: 180, likes: 25 }
  ]);
  console.log('演示作品数据插入完成，作者全部为派大星');
  process.exit();
}

seedPosts();
